package com.example.wdt.wdt;

import cn.wangdian.erp.sdk.Pager;
import cn.wangdian.erp.sdk.api.sales.StockOutAPI;
import cn.wangdian.erp.sdk.api.sales.dto.StockOutQueryRequest;
import cn.wangdian.erp.sdk.api.sales.dto.StockOutQueryResponse;
import cn.wangdian.erp.sdk.impl.Api;
import cn.wangdian.erp.sdk.impl.ApiFactory;
import cn.wangdian.erp.sdk.impl.DefaultClient;
import com.example.wdt.common.controller.ResponseResult;
import com.example.wdt.wdt.statistics.SalesStatisticsService;
import com.example.wdt.wdt.statistics.dto.DateRequest;
import com.example.wdt.wdt.statistics.dto.StatisticsResultDTO;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.wangdian.erp.sdk.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@RestController
public class WdtController {

    private static final Logger log = LoggerFactory.getLogger(WdtController.class);

    private Client client= DefaultClient.get("daixi3", "http://wdt.wangdian.cn", "daixi3-gw",
            "399d7e0cc3e4db4d86ab64045d1b50d1:46822471938776245fbdfbc14a4d4b09");

    @Autowired
    private SalesStatisticsService salesStatisticsService;

    @PostMapping("/wtd")
    public Object gotoWdt(@RequestBody String jsonString,  @RequestParam(name = "method") String methodName, @RequestParam(name = "page_size", required = false, defaultValue = "") Integer pageSize, @RequestParam(name = "page_no", required = false, defaultValue = "") Integer pageNo,@RequestParam(name = "calc_total", required = false, defaultValue = "") Boolean calcTotal)
    {

        Method proxyMethod = WtdApiReflector.getMethod(methodName);
        Type returnType = proxyMethod.getGenericReturnType();

        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
        Object[] argsArray = new Object[]{jsonObject};

        Pager pager=null;
        if (proxyMethod.getAnnotation(Api.class).paged()) {
            pager = new Pager(pageSize, pageNo, calcTotal);
        }

        try{
            Object result =client.execute(methodName,argsArray,pager,returnType);
            ResponseResult response =new ResponseResult(true,0,"success",result);
            return gson.toJson(response);
        }catch (Exception e){
            Map<String,String>  errorResponse=new HashMap<>();
            ResponseResult response = ResponseResult.error();
            response.put(ResponseResult.MSG_TAG,e.getMessage());
            return gson.toJson(response);
        }

    }

    @PostMapping("/sales_statistics")
    @ResponseBody
    public ResponseResult querySalesStatistics(@RequestBody DateRequest date)
    {

        List<StatisticsResultDTO> result=salesStatisticsService.queryStatistics(client,LocalDateTime.parse(date.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        ResponseResult response = ResponseResult.success(result);
        return response;
    }


}
